<?php

namespace Biz\model\activity;


class BizProjectResponsibilityModel extends BizProjectBaseModel
{

    const _TABLE_NAME = 'biz_project_responsibility';

    const RESPONSIBILITY_TYPE_SELLER = 0;
    const RESPONSIBILITY_TYPE_MCN = 1;
    const RESPONSIBILITY_TYPE_PLANNER = 2;
    const RESPONSIBILITY_TYPE_MEDIUM = 3;
    const RESPONSIBILITY_TYPE_PM = 4;
    const RESPONSIBILITY_TYPE_OWNER = 5;
    const RESPONSIBILITY_TYPE_FINANCE = 6;
    const RESPONSIBILITY_TYPE_LEGAL = 7;
    const RESPONSIBILITY_TYPE_NAME_MAP = [
        self::RESPONSIBILITY_TYPE_SELLER => 'seller',
        self::RESPONSIBILITY_TYPE_MCN => 'mcn',
        self::RESPONSIBILITY_TYPE_PLANNER => 'planner',
        self::RESPONSIBILITY_TYPE_MEDIUM => 'medium',
        self::RESPONSIBILITY_TYPE_PM => 'pm',
        self::RESPONSIBILITY_TYPE_OWNER => 'owner',
        self::RESPONSIBILITY_TYPE_FINANCE => 'finance',
        self::RESPONSIBILITY_TYPE_LEGAL => 'legal',
    ];
    const RESPONSIBILITY_TYPE_TITLE_MAP = [
        self::RESPONSIBILITY_TYPE_SELLER => '销售',
        self::RESPONSIBILITY_TYPE_MCN => 'MCN',
        self::RESPONSIBILITY_TYPE_PLANNER => '策划',
        self::RESPONSIBILITY_TYPE_MEDIUM => '媒介',
        self::RESPONSIBILITY_TYPE_PM => 'PM',
        self::RESPONSIBILITY_TYPE_FINANCE => '财务',
        self::RESPONSIBILITY_TYPE_LEGAL => '法务',

    ];

    const RESPONSIBILITY_MODULE_PROJECT = 0;
    const RESPONSIBILITY_MODULE_CUSTOMER = 1;
    const RESPONSIBILITY_MODULE_PREPARE = 2;
    use BizProjectTrait;

    static array $admin_id = [];
    static array $project_id = [];

    public static function afterFindAdmin(): array
    {
        return array_values(self::$admin_id);
    }

    public static function afterFindProject(): array
    {
        return array_values(self::$project_id);
    }

    public static function findAdminByProjectId($project_id, $module = self::RESPONSIBILITY_MODULE_PROJECT): array
    {
        $list = $project_id ? self::dataSelect(self::statusCondition() + ['project_id' => $project_id, 'module' => $module]) : [];
        $response = [];
        self::resetAdminId();
        if ($list) {
            foreach ($list as $v) {
                $name = self::RESPONSIBILITY_TYPE_NAME_MAP[$v['type']];
                $response[$v['project_id']][$name][] = $v['admin_id'];
                self::$admin_id[$v['admin_id']] = $v['admin_id'];
            }
        }
        return is_array($project_id) ? $response : ($response[$project_id] ?? []);
    }

    public static function findProjectByAdmin($admin_id, $module = self::RESPONSIBILITY_MODULE_PROJECT, $type = -1): array
    {
        $condition = ['admin_id' => $admin_id, 'module' => $module];
        if ($type != -1) $condition['type'] = $type;
        $list = $admin_id ? self::dataSelect(self::statusCondition() + $condition) : [];
        $response = [];
        self::resetProjectId();
        if ($list) {
            foreach ($list as $v) {
                $response[$v['admin_id']][$v['project_id']][] = $v['type'];
                self::$project_id[$v['project_id']] = $v['project_id'];
            }
        }
        return is_array($admin_id) ? $response : ($response[$admin_id] ?? []);
    }

    public static function existsList($project_id, $module = self::RESPONSIBILITY_MODULE_PROJECT)
    {
        return $project_id ? self::dataSelect(['project_id' => $project_id, 'module' => $module]) : [];
    }

    public static function normalList($project_id, $module = self::RESPONSIBILITY_MODULE_PROJECT)
    {
        return $project_id ? self::dataSelect(['status' => self::STATUS_NORMAL, 'project_id' => $project_id, 'module' => $module]) : [];
    }

    public static function resetProjectId()
    {
        self::$project_id = [];
    }

    public static function resetAdminId()
    {
        self::$admin_id = [];
    }
}
